package com.android.aiod.serialport;

import android.os.SystemClock;

import com.android.baselibrary.tool.Log;
import com.android.aiod.event.Event_ReceiveData;

import org.greenrobot.eventbus.EventBus;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 读串口线程
 */
public class SerialReadThread extends Thread {

    private static final String TAG = "SerialReadThread";

    private BufferedInputStream mInputStream;

    public SerialReadThread(InputStream is) {
        mInputStream = new BufferedInputStream(is);
    }

    @Override
    public void run() {
        byte[] received = new byte[1024];
        int size;

        Log.e("开始读线程");
        while (true) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            try {
                int available = mInputStream.available();
                if (available > 0) {
                    size = mInputStream.read(received);
                    if (size > 0) {
                        onDataReceive(received, size);
                    }
                } else {
                    // 暂停一点时间，免得一直循环造成CPU占用率过高
                    SystemClock.sleep(1);
                }
            } catch (IOException e) {
                Log.e("读取数据失败", e);
            }
            //Thread.yield();
        }
        Log.e("结束读进程");
    }

    /**
     * 处理获取到的数据
     *
     * @param received
     * @param size
     */
    private void onDataReceive(byte[] received, int size) {
        try {
            byte[] bytes_result = new byte[size];
            System.arraycopy(received, 0, bytes_result, 0, size);

            // TODO: 2018/3/22 解决粘包、分包等
            Event_ReceiveData receiveData = new Event_ReceiveData();
            receiveData.received = bytes_result;
            receiveData.size = size;
            EventBus.getDefault().post(receiveData);
        }catch (Exception e)
        {

        }
    }

    /**
     * 停止读线程
     */
    public void close() {

        try {
            mInputStream.close();
        } catch (IOException e) {
            Log.e("异常", e);
        } finally {
            super.interrupt();
        }
    }
}
